{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Trajectory Consistency Distillation-LoRA"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Trajectory Consistency Distillation (TCD) enables a model to generate higher quality and more detailed images with fewer steps. Moreover, owing to the effective error mitigation during the distillation process, TCD demonstrates superior performance even under conditions of large inference steps.\n",
    "\n",
    "The major advantages of TCD are:\n",
    "\n",
    "- Better than Teacher: TCD demonstrates superior generative quality at both small and large inference steps and exceeds the performance of [DPM-Solver++(2S)](https://huggingface.co/docs/diffusers/main/en/using-diffusers/../../api/schedulers/multistep_dpm_solver) with Stable Diffusion XL (SDXL). There is no additional discriminator or LPIPS supervision included during TCD training.\n",
    "\n",
    "- Flexible Inference Steps: The inference steps for TCD sampling can be freely adjusted without adversely affecting the image quality.\n",
    "\n",
    "- Freely change detail level: During inference, the level of detail in the image can be adjusted with a single hyperparameter, *gamma*.\n",
    "\n",
    "> [!TIP]\n",
    "> For more technical details of TCD, please refer to the [paper](https://huggingface.co/papers/2402.19159) or official [project page](https://mhh0318.github.io/tcd/).\n",
    "\n",
    "For large models like SDXL, TCD is trained with [LoRA](https://huggingface.co/docs/peft/conceptual_guides/adapter#low-rank-adaptation-lora) to reduce memory usage. This is also useful because you can reuse LoRAs between different finetuned models, as long as they share the same base model, without further training.\n",
    "\n",
    "\n",
    "\n",
    "This guide will show you how to perform inference with TCD-LoRAs for a variety of tasks like text-to-image and inpainting, as well as how you can easily combine TCD-LoRAs with other adapters. Choose one of the supported base model and it's corresponding TCD-LoRA checkpoint from the table below to get started.\n",
    "\n",
    "| Base model                                                                                      | TCD-LoRA checkpoint                                            |\n",
    "|-------------------------------------------------------------------------------------------------|----------------------------------------------------------------|\n",
    "| [stable-diffusion-v1-5](https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5)                  | [TCD-SD15](https://huggingface.co/h1t/TCD-SD15-LoRA)           |\n",
    "| [stable-diffusion-2-1-base](https://huggingface.co/stabilityai/stable-diffusion-2-1-base)       | [TCD-SD21-base](https://huggingface.co/h1t/TCD-SD21-base-LoRA) |\n",
    "| [stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0) | [TCD-SDXL](https://huggingface.co/h1t/TCD-SDXL-LoRA)           |\n",
    "\n",
    "\n",
    "Make sure you have [PEFT](https://github.com/huggingface/peft) installed for better LoRA support.\n",
    "\n",
    "```bash\n",
    "pip install -U peft\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## General tasks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this guide, let's use the [StableDiffusionXLPipeline](https://huggingface.co/docs/diffusers/main/en/api/pipelines/stable_diffusion/stable_diffusion_xl#diffusers.StableDiffusionXLPipeline) and the [TCDScheduler](https://huggingface.co/docs/diffusers/main/en/api/schedulers/tcd#diffusers.TCDScheduler). Use the [load_lora_weights()](https://huggingface.co/docs/diffusers/main/en/api/loaders/lora#diffusers.loaders.StableDiffusionLoraLoaderMixin.load_lora_weights) method to load the SDXL-compatible TCD-LoRA weights.\n",
    "\n",
    "A few tips to keep in mind for TCD-LoRA inference are to:\n",
    "\n",
    "- Keep the `num_inference_steps` between 4 and 50\n",
    "- Set `eta` (used to control stochasticity at each step) between 0 and 1. You should use a higher `eta` when increasing the number of inference steps, but the downside is that a larger `eta` in [TCDScheduler](https://huggingface.co/docs/diffusers/main/en/api/schedulers/tcd#diffusers.TCDScheduler) leads to blurrier images. A value of 0.3 is recommended to produce good results.\n",
    "\n",
    "<hfoptions id=\"tasks\">\n",
    "<hfoption id=\"text-to-image\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from diffusers import StableDiffusionXLPipeline, TCDScheduler\n",
    "\n",
    "device = \"cuda\"\n",
    "base_model_id = \"stabilityai/stable-diffusion-xl-base-1.0\"\n",
    "tcd_lora_id = \"h1t/TCD-SDXL-LoRA\"\n",
    "\n",
    "pipe = StableDiffusionXLPipeline.from_pretrained(base_model_id, torch_dtype=torch.float16, variant=\"fp16\").to(device)\n",
    "pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)\n",
    "\n",
    "pipe.load_lora_weights(tcd_lora_id)\n",
    "pipe.fuse_lora()\n",
    "\n",
    "prompt = \"Painting of the orange cat Otto von Garfield, Count of Bismarck-Schönhausen, Duke of Lauenburg, Minister-President of Prussia. Depicted wearing a Prussian Pickelhaube and eating his favorite meal - lasagna.\"\n",
    "\n",
    "image = pipe(\n",
    "    prompt=prompt,\n",
    "    num_inference_steps=4,\n",
    "    guidance_scale=0,\n",
    "    eta=0.3,\n",
    "    generator=torch.Generator(device=device).manual_seed(0),\n",
    ").images[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://github.com/jabir-zheng/TCD/raw/main/assets/demo_image.png)\n",
    "\n",
    "</hfoption>\n",
    "\n",
    "<hfoption id=\"inpainting\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from diffusers import AutoPipelineForInpainting, TCDScheduler\n",
    "from diffusers.utils import load_image, make_image_grid\n",
    "\n",
    "device = \"cuda\"\n",
    "base_model_id = \"diffusers/stable-diffusion-xl-1.0-inpainting-0.1\"\n",
    "tcd_lora_id = \"h1t/TCD-SDXL-LoRA\"\n",
    "\n",
    "pipe = AutoPipelineForInpainting.from_pretrained(base_model_id, torch_dtype=torch.float16, variant=\"fp16\").to(device)\n",
    "pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)\n",
    "\n",
    "pipe.load_lora_weights(tcd_lora_id)\n",
    "pipe.fuse_lora()\n",
    "\n",
    "img_url = \"https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png\"\n",
    "mask_url = \"https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png\"\n",
    "\n",
    "init_image = load_image(img_url).resize((1024, 1024))\n",
    "mask_image = load_image(mask_url).resize((1024, 1024))\n",
    "\n",
    "prompt = \"a tiger sitting on a park bench\"\n",
    "\n",
    "image = pipe(\n",
    "  prompt=prompt,\n",
    "  image=init_image,\n",
    "  mask_image=mask_image,\n",
    "  num_inference_steps=8,\n",
    "  guidance_scale=0,\n",
    "  eta=0.3,\n",
    "  strength=0.99,  # make sure to use `strength` below 1.0\n",
    "  generator=torch.Generator(device=device).manual_seed(0),\n",
    ").images[0]\n",
    "\n",
    "grid_image = make_image_grid([init_image, mask_image, image], rows=1, cols=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://github.com/jabir-zheng/TCD/raw/main/assets/inpainting_tcd.png)\n",
    "\n",
    "\n",
    "</hfoption>\n",
    "</hfoptions>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Community models"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "TCD-LoRA also works with many community finetuned models and plugins. For example, load the [animagine-xl-3.0](https://huggingface.co/cagliostrolab/animagine-xl-3.0) checkpoint which is a community finetuned version of SDXL for generating anime images."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from diffusers import StableDiffusionXLPipeline, TCDScheduler\n",
    "\n",
    "device = \"cuda\"\n",
    "base_model_id = \"cagliostrolab/animagine-xl-3.0\"\n",
    "tcd_lora_id = \"h1t/TCD-SDXL-LoRA\"\n",
    "\n",
    "pipe = StableDiffusionXLPipeline.from_pretrained(base_model_id, torch_dtype=torch.float16, variant=\"fp16\").to(device)\n",
    "pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)\n",
    "\n",
    "pipe.load_lora_weights(tcd_lora_id)\n",
    "pipe.fuse_lora()\n",
    "\n",
    "prompt = \"A man, clad in a meticulously tailored military uniform, stands with unwavering resolve. The uniform boasts intricate details, and his eyes gleam with determination. Strands of vibrant, windswept hair peek out from beneath the brim of his cap.\"\n",
    "\n",
    "image = pipe(\n",
    "    prompt=prompt,\n",
    "    num_inference_steps=8,\n",
    "    guidance_scale=0,\n",
    "    eta=0.3,\n",
    "    generator=torch.Generator(device=device).manual_seed(0),\n",
    ").images[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://github.com/jabir-zheng/TCD/raw/main/assets/animagine_xl.png)\n",
    "\n",
    "TCD-LoRA also supports other LoRAs trained on different styles. For example, let's load the [TheLastBen/Papercut_SDXL](https://huggingface.co/TheLastBen/Papercut_SDXL) LoRA and fuse it with the TCD-LoRA with the `~loaders.UNet2DConditionLoadersMixin.set_adapters` method.\n",
    "\n",
    "> [!TIP]\n",
    "> Check out the [Merge LoRAs](https://huggingface.co/docs/diffusers/main/en/using-diffusers/merge_loras) guide to learn more about efficient merging methods."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from diffusers import StableDiffusionXLPipeline\n",
    "from scheduling_tcd import TCDScheduler\n",
    "\n",
    "device = \"cuda\"\n",
    "base_model_id = \"stabilityai/stable-diffusion-xl-base-1.0\"\n",
    "tcd_lora_id = \"h1t/TCD-SDXL-LoRA\"\n",
    "styled_lora_id = \"TheLastBen/Papercut_SDXL\"\n",
    "\n",
    "pipe = StableDiffusionXLPipeline.from_pretrained(base_model_id, torch_dtype=torch.float16, variant=\"fp16\").to(device)\n",
    "pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)\n",
    "\n",
    "pipe.load_lora_weights(tcd_lora_id, adapter_name=\"tcd\")\n",
    "pipe.load_lora_weights(styled_lora_id, adapter_name=\"style\")\n",
    "pipe.set_adapters([\"tcd\", \"style\"], adapter_weights=[1.0, 1.0])\n",
    "\n",
    "prompt = \"papercut of a winter mountain, snow\"\n",
    "\n",
    "image = pipe(\n",
    "    prompt=prompt,\n",
    "    num_inference_steps=4,\n",
    "    guidance_scale=0,\n",
    "    eta=0.3,\n",
    "    generator=torch.Generator(device=device).manual_seed(0),\n",
    ").images[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://github.com/jabir-zheng/TCD/raw/main/assets/styled_lora.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adapters"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "TCD-LoRA is very versatile, and it can be combined with other adapter types like ControlNets, IP-Adapter, and AnimateDiff.\n",
    "\n",
    "<hfoptions id=\"adapters\">\n",
    "<hfoption id=\"ControlNet\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Depth ControlNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "from transformers import DPTImageProcessor, DPTForDepthEstimation\n",
    "from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline\n",
    "from diffusers.utils import load_image, make_image_grid\n",
    "from scheduling_tcd import TCDScheduler\n",
    "\n",
    "device = \"cuda\"\n",
    "depth_estimator = DPTForDepthEstimation.from_pretrained(\"Intel/dpt-hybrid-midas\").to(device)\n",
    "feature_extractor = DPTImageProcessor.from_pretrained(\"Intel/dpt-hybrid-midas\")\n",
    "\n",
    "def get_depth_map(image):\n",
    "    image = feature_extractor(images=image, return_tensors=\"pt\").pixel_values.to(device)\n",
    "    with torch.no_grad(), torch.autocast(device):\n",
    "        depth_map = depth_estimator(image).predicted_depth\n",
    "\n",
    "    depth_map = torch.nn.functional.interpolate(\n",
    "        depth_map.unsqueeze(1),\n",
    "        size=(1024, 1024),\n",
    "        mode=\"bicubic\",\n",
    "        align_corners=False,\n",
    "    )\n",
    "    depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)\n",
    "    depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)\n",
    "    depth_map = (depth_map - depth_min) / (depth_max - depth_min)\n",
    "    image = torch.cat([depth_map] * 3, dim=1)\n",
    "\n",
    "    image = image.permute(0, 2, 3, 1).cpu().numpy()[0]\n",
    "    image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))\n",
    "    return image\n",
    "\n",
    "base_model_id = \"stabilityai/stable-diffusion-xl-base-1.0\"\n",
    "controlnet_id = \"diffusers/controlnet-depth-sdxl-1.0\"\n",
    "tcd_lora_id = \"h1t/TCD-SDXL-LoRA\"\n",
    "\n",
    "controlnet = ControlNetModel.from_pretrained(\n",
    "    controlnet_id,\n",
    "    torch_dtype=torch.float16,\n",
    "    variant=\"fp16\",\n",
    ")\n",
    "pipe = StableDiffusionXLControlNetPipeline.from_pretrained(\n",
    "    base_model_id,\n",
    "    controlnet=controlnet,\n",
    "    torch_dtype=torch.float16,\n",
    "    variant=\"fp16\",\n",
    ")\n",
    "pipe.enable_model_cpu_offload()\n",
    "\n",
    "pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)\n",
    "\n",
    "pipe.load_lora_weights(tcd_lora_id)\n",
    "pipe.fuse_lora()\n",
    "\n",
    "prompt = \"stormtrooper lecture, photorealistic\"\n",
    "\n",
    "image = load_image(\"https://huggingface.co/lllyasviel/sd-controlnet-depth/resolve/main/images/stormtrooper.png\")\n",
    "depth_image = get_depth_map(image)\n",
    "\n",
    "controlnet_conditioning_scale = 0.5  # recommended for good generalization\n",
    "\n",
    "image = pipe(\n",
    "    prompt,\n",
    "    image=depth_image,\n",
    "    num_inference_steps=4,\n",
    "    guidance_scale=0,\n",
    "    eta=0.3,\n",
    "    controlnet_conditioning_scale=controlnet_conditioning_scale,\n",
    "    generator=torch.Generator(device=device).manual_seed(0),\n",
    ").images[0]\n",
    "\n",
    "grid_image = make_image_grid([depth_image, image], rows=1, cols=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://github.com/jabir-zheng/TCD/raw/main/assets/controlnet_depth_tcd.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Canny ControlNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline\n",
    "from diffusers.utils import load_image, make_image_grid\n",
    "from scheduling_tcd import TCDScheduler\n",
    "\n",
    "device = \"cuda\"\n",
    "base_model_id = \"stabilityai/stable-diffusion-xl-base-1.0\"\n",
    "controlnet_id = \"diffusers/controlnet-canny-sdxl-1.0\"\n",
    "tcd_lora_id = \"h1t/TCD-SDXL-LoRA\"\n",
    "\n",
    "controlnet = ControlNetModel.from_pretrained(\n",
    "    controlnet_id,\n",
    "    torch_dtype=torch.float16,\n",
    "    variant=\"fp16\",\n",
    ")\n",
    "pipe = StableDiffusionXLControlNetPipeline.from_pretrained(\n",
    "    base_model_id,\n",
    "    controlnet=controlnet,\n",
    "    torch_dtype=torch.float16,\n",
    "    variant=\"fp16\",\n",
    ")\n",
    "pipe.enable_model_cpu_offload()\n",
    "\n",
    "pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)\n",
    "\n",
    "pipe.load_lora_weights(tcd_lora_id)\n",
    "pipe.fuse_lora()\n",
    "\n",
    "prompt = \"ultrarealistic shot of a furry blue bird\"\n",
    "\n",
    "canny_image = load_image(\"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png\")\n",
    "\n",
    "controlnet_conditioning_scale = 0.5  # recommended for good generalization\n",
    "\n",
    "image = pipe(\n",
    "    prompt,\n",
    "    image=canny_image,\n",
    "    num_inference_steps=4,\n",
    "    guidance_scale=0,\n",
    "    eta=0.3,\n",
    "    controlnet_conditioning_scale=controlnet_conditioning_scale,\n",
    "    generator=torch.Generator(device=device).manual_seed(0),\n",
    ").images[0]\n",
    "\n",
    "grid_image = make_image_grid([canny_image, image], rows=1, cols=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://github.com/jabir-zheng/TCD/raw/main/assets/controlnet_canny_tcd.png)\n",
    "\n",
    "<Tip>\n",
    "The inference parameters in this example might not work for all examples, so we recommend you to try different values for `num_inference_steps`, `guidance_scale`, `controlnet_conditioning_scale` and `cross_attention_kwargs` parameters and choose the best one.\n",
    "</Tip>\n",
    "\n",
    "</hfoption>\n",
    "<hfoption id=\"IP-Adapter\">\n",
    "\n",
    "This example shows how to use the TCD-LoRA with the [IP-Adapter](https://github.com/tencent-ailab/IP-Adapter/tree/main) and SDXL."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from diffusers import StableDiffusionXLPipeline\n",
    "from diffusers.utils import load_image, make_image_grid\n",
    "\n",
    "from ip_adapter import IPAdapterXL\n",
    "from scheduling_tcd import TCDScheduler\n",
    "\n",
    "device = \"cuda\"\n",
    "base_model_path = \"stabilityai/stable-diffusion-xl-base-1.0\"\n",
    "image_encoder_path = \"sdxl_models/image_encoder\"\n",
    "ip_ckpt = \"sdxl_models/ip-adapter_sdxl.bin\"\n",
    "tcd_lora_id = \"h1t/TCD-SDXL-LoRA\"\n",
    "\n",
    "pipe = StableDiffusionXLPipeline.from_pretrained(\n",
    "    base_model_path,\n",
    "    torch_dtype=torch.float16,\n",
    "    variant=\"fp16\"\n",
    ")\n",
    "pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)\n",
    "\n",
    "pipe.load_lora_weights(tcd_lora_id)\n",
    "pipe.fuse_lora()\n",
    "\n",
    "ip_model = IPAdapterXL(pipe, image_encoder_path, ip_ckpt, device)\n",
    "\n",
    "ref_image = load_image(\"https://raw.githubusercontent.com/tencent-ailab/IP-Adapter/main/assets/images/woman.png\").resize((512, 512))\n",
    "\n",
    "prompt = \"best quality, high quality, wearing sunglasses\"\n",
    "\n",
    "image = ip_model.generate(\n",
    "    pil_image=ref_image,\n",
    "    prompt=prompt,\n",
    "    scale=0.5,\n",
    "    num_samples=1,\n",
    "    num_inference_steps=4,\n",
    "    guidance_scale=0,\n",
    "    eta=0.3,\n",
    "    seed=0,\n",
    ")[0]\n",
    "\n",
    "grid_image = make_image_grid([ref_image, image], rows=1, cols=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://github.com/jabir-zheng/TCD/raw/main/assets/ip_adapter.png)\n",
    "\n",
    "\n",
    "\n",
    "</hfoption>\n",
    "<hfoption id=\"AnimateDiff\">\n",
    "\n",
    "`AnimateDiff` allows animating images using Stable Diffusion models. TCD-LoRA can substantially accelerate the process without degrading image quality. The quality of animation with TCD-LoRA and AnimateDiff has a more lucid outcome."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from diffusers import MotionAdapter, AnimateDiffPipeline, DDIMScheduler\n",
    "from scheduling_tcd import TCDScheduler\n",
    "from diffusers.utils import export_to_gif\n",
    "\n",
    "adapter = MotionAdapter.from_pretrained(\"guoyww/animatediff-motion-adapter-v1-5\")\n",
    "pipe = AnimateDiffPipeline.from_pretrained(\n",
    "    \"frankjoshua/toonyou_beta6\",\n",
    "    motion_adapter=adapter,\n",
    ").to(\"cuda\")\n",
    "\n",
    "# set TCDScheduler\n",
    "pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)\n",
    "\n",
    "# load TCD LoRA\n",
    "pipe.load_lora_weights(\"h1t/TCD-SD15-LoRA\", adapter_name=\"tcd\")\n",
    "pipe.load_lora_weights(\"guoyww/animatediff-motion-lora-zoom-in\", weight_name=\"diffusion_pytorch_model.safetensors\", adapter_name=\"motion-lora\")\n",
    "\n",
    "pipe.set_adapters([\"tcd\", \"motion-lora\"], adapter_weights=[1.0, 1.2])\n",
    "\n",
    "prompt = \"best quality, masterpiece, 1girl, looking at viewer, blurry background, upper body, contemporary, dress\"\n",
    "generator = torch.manual_seed(0)\n",
    "frames = pipe(\n",
    "    prompt=prompt,\n",
    "    num_inference_steps=5,\n",
    "    guidance_scale=0,\n",
    "    cross_attention_kwargs={\"scale\": 1},\n",
    "    num_frames=24,\n",
    "    eta=0.3,\n",
    "    generator=generator\n",
    ").frames[0]\n",
    "export_to_gif(frames, \"animation.gif\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://github.com/jabir-zheng/TCD/raw/main/assets/animation_example.gif)\n",
    "\n",
    "</hfoption>\n",
    "</hfoptions>"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 4
}
